package br.com.hmcontabilidade.onlinev3.view;

import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import br.com.hmcontabilidade.onlinev3.business.EmpresaBC;
import br.com.hmcontabilidade.onlinev3.domain.Empresa;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;

@ViewController
@NextView("/empresa_edit.xhtml")
@PreviousView("/empresa_list.xhtml")
public class EmpresaListMB extends AbstractListPageBean<Empresa, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private EmpresaBC bc;

	@Override
	protected List<Empresa> handleResultList() {
		return this.bc.findAll();
	}

	@Transactional
	public String deleteSelection() {
		boolean delete;
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);

			if (delete) {
				bc.delete(id);
				iter.remove();
			}
		}
		return getPreviousView();
	}
	
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Empresa Selecionado", ((Empresa) event.getObject()).getNomeFantasia());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
  
    public void onRowUnselect(UnselectEvent event) {  
        FacesMessage msg = new FacesMessage("Empresa Desmarcado", ((Empresa) event.getObject()).getNomeFantasia());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }

}
